﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Data;

using GroundWater.Data;
using GroundWater.BusinessActions;
using MonitoringTable = GroundWater.Data.MonitoringDataSet.MonitoringDataTable;
using MonitoringRow = GroundWater.Data.MonitoringDataSet.MonitoringRow;

using WellTable = GroundWater.Data.InfoDataSet.WellsDataTable;
using WellRow = GroundWater.Data.InfoDataSet.WellsRow;


namespace GroundWater.analysis
{
    public partial class WaterlevelContrast : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SiteUtil.Validate(this, "水位及埋深同期对比");
            if (!this.IsPostBack)
            {
                //txtStartDateB.Text = DateTime.Now.AddYears(-1).AddMonths(-1).ToString("yyyy-MM-dd");
                //txtEndDateB.Text = DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd");

                txtStartDateB.Text = DateTime.Now.AddMonths(-2).AddDays(1).ToString("yyyy-MM-dd");
                txtEndDateB.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");

                txtStartDateA.Text = DateTime.Now.AddMonths(-1).AddDays(1).ToString("yyyy-MM-dd");
                txtEndDateA.Text = DateTime.Now.ToString("yyyy-MM-dd");

                drpSectionPlane.DataBind();
                drpWell.DataBind();
                btnAnalysis_Click(null, EventArgs.Empty);
            }

            
        }


        protected void btnAnalysis_Click(object sender, EventArgs e)
        {
            //RegisterDataItem();
            DateTime startB = DateTime.Parse(txtStartDateB.Text);
            DateTime endB = DateTime.Parse(txtEndDateB.Text);
            DateTime startA = DateTime.Parse(txtStartDateA.Text);
            DateTime endA = DateTime.Parse(txtEndDateA.Text);

            if (endB < startB)
            {
                RegisterClientScript("alert('前期的截至日期必须大于等于起始日期。');", lblJavaScript);
                return;
            }
            if (endA < startA)
            {
                RegisterClientScript("alert('后期的截至日期必须大于等于起始日期。');", lblJavaScript);
                return;
            }

            if (startA < endB)
            {
                RegisterClientScript("alert('后期的起始日期必须大于等于前期的截至日期。');", lblJavaScript);
                return;
            }

            GenerateImage();

        }

        protected void drpWell_SelectedIndexChanged(object sender, EventArgs e)
        {
            //RegisterDataItem();
        }

        protected void drpWell_DataBound(object sender, EventArgs e)
        {

        }

        private void GenerateImage()
        {
            Chart chart = chartWaterLevel;
            ChartArea waterLevelArea = chartWaterLevel.ChartAreas["ChartArea1"];
            ChartArea altitudeLevelArea = chartWaterLevel.ChartAreas["ChartArea2"];

            Series waterlevelB = chartWaterLevel.Series["前期水位"];
            Series waterlevelA = chartWaterLevel.Series["本期水位"];
            Series altitudeB = chartWaterLevel.Series["前期埋深"];
            Series altitudeA = chartWaterLevel.Series["本期埋深"];

            waterlevelB["DrawingStyle"] = "Cylinder";
            waterlevelA["DrawingStyle"] = "Cylinder";
            altitudeB["DrawingStyle"] = "Cylinder";
            altitudeA["DrawingStyle"] = "Cylinder";

            waterLevelArea.AxisY.TitleFont = new System.Drawing.Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold);
            altitudeLevelArea.AxisY.TitleFont = new System.Drawing.Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold);

            waterLevelArea.AxisY.Title = "水位 (米)";
            altitudeLevelArea.AxisY.Title = "埋深 (米)";

            decimal waterlevelBefore = 0m;
            decimal waterlevelAfter = 0m;
            DataTable wlTableB = Monitoring.FetchWaterLeveByWellCode(drpWell.SelectedValue, txtStartDateB.Text, txtEndDateB.Text);

            if (wlTableB == null || wlTableB.Rows.Count == 0)
            {

            }
            else
            {
                waterlevelBefore = Convert.ToDecimal(wlTableB.Rows[0]["Waterlevel"]);
            }
            DataTable wlTableA = Monitoring.FetchWaterLeveByWellCode(drpWell.SelectedValue, txtStartDateA.Text, txtEndDateA.Text);
            if (wlTableA == null || wlTableA.Rows.Count == 0)
            {

            }
            else
            {
                waterlevelAfter = Convert.ToDecimal(wlTableA.Rows[0]["Waterlevel"]);
            }


            decimal planeBefore = 0m;
            decimal planeAfter = 0m;
            DataTable spTableB = Monitoring.FetchWaterLeveBySectionPlane(drpSectionPlane.SelectedItem.Text, txtStartDateB.Text, txtEndDateB.Text);

            if (spTableB == null || spTableB.Rows.Count == 0)
            {

            }
            else
            {
                planeBefore = Convert.ToDecimal(spTableB.Rows[0]["Waterlevel"]);
            }
            DataTable spTableA = Monitoring.FetchWaterLeveBySectionPlane(drpSectionPlane.SelectedItem.Text, txtStartDateA.Text, txtEndDateA.Text);
            if (spTableA == null || spTableA.Rows.Count == 0)
            {

            }
            else
            {
                planeAfter = Convert.ToDecimal(spTableA.Rows[0]["Waterlevel"]);
            }

            decimal valleyBefore = 0m;
            decimal valleyAfter = 0m;
            DataTable vTableB = Monitoring.FetchWaterLeveAllBySectionPlane( txtStartDateB.Text, txtEndDateB.Text);

            if (vTableB == null || vTableB.Rows.Count == 0)
            {

            }
            else
            {
                var rows = vTableB.AsEnumerable();
                valleyBefore = Math.Round( rows.Average(r => Convert.ToDecimal(r["Waterlevel"])), 2);

            }
            DataTable vTableA = Monitoring.FetchWaterLeveAllBySectionPlane(txtStartDateA.Text, txtEndDateA.Text);
            if (vTableA == null || vTableA.Rows.Count == 0)
            {

            }
            else
            {
                var rows = vTableA.AsEnumerable();
                valleyAfter = Math.Round(rows.Average(r => Convert.ToDecimal(r["Waterlevel"])), 2);
            }


            DataPoint wb = new DataPoint(1d,  Convert.ToDouble(waterlevelBefore));
            wb.AxisLabel = "水井";
            wb.Label = waterlevelBefore.ToString();
            waterlevelB.Points.Add(wb);

            DataPoint wa = new DataPoint(1d, Convert.ToDouble(waterlevelAfter));
            wa.AxisLabel = "水井";
            wa.Label = waterlevelAfter.ToString();
            waterlevelA.Points.Add(wa);

            DataPoint pb = new DataPoint(2d, Convert.ToDouble(planeBefore));
            pb.AxisLabel = "剖面";
            pb.Label = planeBefore.ToString();
            waterlevelB.Points.Add(pb);

            DataPoint pa = new DataPoint(2d, Convert.ToDouble(planeAfter));
            pa.AxisLabel = "剖面";
            pa.Label = planeAfter.ToString();
            waterlevelA.Points.Add(pa);

            DataPoint vb = new DataPoint(3d, Convert.ToDouble(valleyBefore));
            vb.AxisLabel = "流域";
            vb.Label = valleyBefore.ToString();
            waterlevelB.Points.Add(vb);

            DataPoint va = new DataPoint(3d, Convert.ToDouble(valleyAfter));
            va.AxisLabel = "流域";
            va.Label = valleyAfter.ToString();
            waterlevelA.Points.Add(va);



            WellTable wells = Well.GetAll(false, true);
            var wellRows = wells.AsEnumerable();
            decimal valleyAltitude = wellRows.Average(r => r.Altitude);

            var sectionWells = wellRows.Where(r => r.SectionPlane == drpSectionPlane.SelectedItem.Text);
            decimal planeAltitude = sectionWells.Average(r => r.Altitude);

            double wellAltitudeBefore = 0d;
            double wellAltitudeAfter = 0d;
            double planeAltitudeBefore = 0d;
            double planeAltitudeAfter = 0d;
            double valleyAltitudeBefore = 0d;
            double valleyAltitudeAfter = 0d;

            WellRow well = Well.GetByIdFromCache(drpWell.SelectedValue, false);
            wellAltitudeBefore = well.Altitude == 0 ? 0d: Convert.ToDouble(well.Altitude - waterlevelBefore);
            wellAltitudeAfter = well.Altitude == 0 ? 0d : Convert.ToDouble(well.Altitude - waterlevelAfter);

            planeAltitudeBefore = planeAltitude - planeBefore <= 0 ? 0d : Math.Round(Convert.ToDouble(planeAltitude - planeBefore), 2);
            planeAltitudeAfter = planeAltitude - planeAfter <= 0 ? 0d : Math.Round(Convert.ToDouble(planeAltitude - planeAfter), 2);

            valleyAltitudeBefore = valleyAltitude - valleyBefore <= 0 ? 0d : Math.Round(Convert.ToDouble(valleyAltitude - valleyBefore), 2);
            valleyAltitudeAfter = valleyAltitude - valleyAfter <= 0 ? 0d : Math.Round(Convert.ToDouble(valleyAltitude - valleyAfter), 2);


            DataPoint wba = new DataPoint(1d, wellAltitudeBefore);
            wba.AxisLabel = "水井";
            wba.Label = wellAltitudeBefore.ToString();
            altitudeB.Points.Add(wba);

            DataPoint waa = new DataPoint(1d, wellAltitudeAfter);
            waa.AxisLabel = "水井";
            waa.Label = wellAltitudeAfter.ToString();
            altitudeA.Points.Add(waa);

            DataPoint pba = new DataPoint(2d, planeAltitudeBefore);
            pba.AxisLabel = "剖面";
            pba.Label = planeAltitudeBefore.ToString();
            altitudeB.Points.Add(pba);

            DataPoint paa = new DataPoint(2d, planeAltitudeAfter);
            paa.AxisLabel = "剖面";
            paa.Label = planeAltitudeAfter.ToString();
            altitudeA.Points.Add(paa);

            DataPoint vba = new DataPoint(3d, valleyAltitudeBefore);
            vba.AxisLabel = "流域";
            vba.Label = valleyAltitudeBefore.ToString();
            altitudeB.Points.Add(vba);

            DataPoint vaa = new DataPoint(3d, valleyAltitudeAfter);
            vaa.AxisLabel = "流域";
            vaa.Label = valleyAltitudeAfter.ToString();
            altitudeA.Points.Add(vaa);
        }


    }
}
